package com.gcb.io;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

public class GetNewFile {
	
	private static List<File> list = new ArrayList<File>();
	private static String baseDir;
	private static String descDir;
	private static Date comDate;
	
	public GetNewFile(String src, Date date){
		baseDir = src;
		comDate = date;
	}
	
	public GetNewFile(String src, String desc, Date date){
		baseDir = src;
		descDir = desc;
		comDate = date;
	}
	
	public void getNewFile(){
		File file = new File(baseDir);
		listFile(file);
		newFile(list, comDate);
		try {
			createFile(list, file, descDir);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void getNewFile(String desc){
		File file = new File(baseDir);
		listFile(file);
		newFile(list, comDate);
		try {
			createFile(list, file, desc);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 将指定文件路径下面的文件及目录遍历，有递归
	 * @param file void
	 * @author Cre.Gu at 2012-3-22
	 */
	public void listFile(File file){
		File[] files = file.listFiles();
		for (File file2 : files) {
			if(file2.isDirectory()){
				listFile(file2);
			}
			list.add(file2);
		}
	}
	
	/**
	 * 找出此时间之后改动的文件
	 * @param listFile	文件列表
	 * @param compareDate	比较的时间
	 * @return boolean	
	 * @author Creat at 2012-3-22
	 */
	public boolean newFile(List<File> listFile, Date compareDate){
		for (Iterator<File> iterator = listFile.iterator(); iterator.hasNext();) {
			File file = (File) iterator.next();
			if(compareDate.after(new Date(file.lastModified()))){
				iterator.remove();
			}
		}
		return true;
	}
	
	public boolean createFile(List<File> listFile, File base, String desc) throws IOException{
		File file1 = new File(desc);
		if(!file1.exists())
			file1.mkdirs();
		for (Iterator<File> iterator = listFile.iterator(); iterator.hasNext();) {
			File file = (File) iterator.next();
			String childName = file.getCanonicalPath().substring(file.getCanonicalPath().indexOf(base.getName()));
			File descFile = new File(desc, childName);
			System.out.println(descFile);
			if(descFile.isDirectory()){
				descFile.mkdirs();
			}else{
				try {
					DataHandler.copyFile(file, descFile);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return true;
	}
	
	public void printList(List<File> list){
		for (Iterator<File> iterator = list.iterator(); iterator.hasNext();) {
			File file = (File) iterator.next();
			System.out.println(file);
		}
	}
	
	/**
	 * @param args void
	 * @author Creat at 2012-3-22
	 * @throws ParseException 
	 */
	public static void main(String[] args) throws ParseException {
		// TODO Auto-generated method stub
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date date = sdf.parse("2012-3-22 10:00");
		String src = "F:\\TestCopy";
		GetNewFile gnf = new GetNewFile(src, date);
		gnf.getNewFile("C:\\20120323");
//		//gnf.printList(list);
//		File file = new File("C:\\20120323\\TestCopy\\个人聊天信息.txt");
//		System.out.println(file.exists());
//		System.out.println(file.mkdir());
	}
	

}
